---
- hosts: "LB"
  remote_user: root
  gather_facts: yes
  connection: ssh

  tasks:
  
    - name: Ensure dependencies are installed
      yum: name={{ item }} state=present
      with_items:
        - openssl-devel 
        - pcre-devel
        - make
        - gcc
        - socat
        - haproxy
        - keepalived
      when: ansible_os_family == "RedHat"

    - name: Ensure dependencies are installed
      apt: name={{ item }} state=present
      with_items:
        - build-essential
        - zlib1g-dev 
        - libpcre3-dev
        - libssl-dev
        - socat
        - haproxy
        - keepalived
      when: ansible_os_family == "Debian"

    - name: Ensure haproxy conf is set
      template: 
        src: templates/haproxy.cfg.j2 
        dest: /etc/haproxy/haproxy.cfg

    - name: Ensure keepalived conf is set
      template: 
        src: templates/keepalived.conf.j2
        dest: /etc/keepalived/keepalived.conf

    - name: Ensure keepalived check set
      template: 
        src: templates/check_apiserver.sh.j2
        dest: /etc/keepalived/check_apiserver.sh

    - name: Ensure haproxy is started
      service:  
        name: haproxy
        state: started
        enabled: yes

    - name: Ensure keepalived is started
      service:  
        name: keepalived
        state: started
        enabled: yes